package com.jule.teenpatti.game.network.protocol.reqs;

import JoloProtobuf.GameSvr.JoloGame;
import com.jule.teenpatti.game.gameUtil.GameLogic;
import com.jule.teenpatti.game.model.PlayerInfo;
import com.jule.teenpatti.game.model.eenum.PlayerStateEnum;
import com.jule.teenpatti.game.network.protocol.ClientReq;
import com.jule.teenpatti.game.network.protocol.acks.JoloGame_GamePrepareAck_50064;
import com.jule.teenpatti.game.play.AbstractTable;
import com.jule.teenpatti.game.service.TableService;
import com.jule.teenpatti.game.vavle.notice.NoticeBroadcastMessages;
import io.netty.buffer.ByteBuf;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class JoloGame_GamePrepareReq_50064 extends ClientReq {
    private long time;
    private JoloGame.JoloGame_GamePrepareReq req;

    public JoloGame_GamePrepareReq_50064(int functionId) {
        super(functionId);
    }

    @Override
    public void readPayLoadImpl(ByteBuf buf) throws Exception {
        time = System.currentTimeMillis();
        byte[] blob = new byte[buf.readableBytes()];
        buf.readBytes(blob);
        req = JoloGame.JoloGame_GamePrepareReq.parseFrom(blob);
        this.setTable(TableService.getInstance().getTable(header.gameId + "", req.getRoomId(), req.getTableId()));
    }

    @Override
    public void processImpl() throws Exception {
        try {
            log.debug("收到消息-> " + functionId + " reqNum-> " + header.reqNum + " " + req.toString());
            String userId = req.getUserId();
            String roomId = req.getRoomId();
            String tableId = req.getTableId();

            JoloGame.JoloGame_GamePrepareAck.Builder ack = JoloGame.JoloGame_GamePrepareAck.newBuilder();
            ack.setUserId(userId);
            ack.setRoomId(roomId);
            ack.setTableId(tableId);

            AbstractTable table = getTable();
            PlayerInfo player = table.getPlayer(userId);
            if(player != null){
                //设置玩家状态为准备
                player.setState(PlayerStateEnum.prepare);
                ack.setSeatNum(player.getSeatNum());
                ack.setResult(1);
                ctx.writeAndFlush(new JoloGame_GamePrepareAck_50064(ack.build(), header));
                NoticeBroadcastMessages.gamePrepare(table,player);
            }else{
                ack.setSeatNum(-1);
                ack.setResult(-1);
                ack.setResultMsg("players do not exist.");
                ctx.writeAndFlush(new JoloGame_GamePrepareAck_50064(ack.build(), header));
            }
            isGameStart(table);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        }finally {

        }
    }

    //每当一个玩家准备之后  就判断一下桌面是否全部人准备了 如果是则直接终止定时器开始游戏
    public static void isGameStart(AbstractTable table){
        for(PlayerInfo playerInfo : table.getAllPlayers().values()){
            log.debug("===================="+playerInfo.getState());
            if(playerInfo.getState() != PlayerStateEnum.prepare){
                return;
            }
        }
        log.debug("当前桌子的状态为："+table.getTableStateEnum());
        GameLogic.gameStart(table);
    }
}
